package cn.fenqing168.springcloud.controller;

import cn.fenqing168.springcloud.commons.BaseResult;
import cn.fenqing168.springcloud.myheadler.CommonHeadler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fenqing
 */
@RestController
@Slf4j
public class CurrentLimitTestController {

    /**
     * 测试根据资源名称限流测试
     * @return 响应
     */
    @GetMapping("/limit/byResource")
    @SentinelResource(value = "myResource", blockHandler = "byResourceBlockHandler")
    public BaseResult<?> byResource(){
        return BaseResult.success("测试通过资源名称限流，服务处理成功！");
    }

    /**
     * 限流后的兜底方法
     * @param e 限流产生的异常
     * @return 被限流厚后的友好响应
     */
    public BaseResult<?> byResourceBlockHandler(BlockException e){
        return BaseResult.error("请求被限流，异常类为：" + e.getClass().getCanonicalName());
    }

    /**
     * 测试通过url限流
     * @return 正常的返回
     */
    @GetMapping("/limit/byUrl")
    public BaseResult<?> byUrl(){
        return BaseResult.success("测试通过URL限流，服务处理成功！");
    }

    /**
     * 测试全局限流兜底方法
     * @return 正常返回
     */
    @GetMapping("/limit/common")
    @SentinelResource(value = "common", blockHandler = "commonHeadler", blockHandlerClass = CommonHeadler.class)
    public BaseResult<?> common(){
        return BaseResult.success("测试全局限流方法成功");
    }

    public BaseResult commonHeadler(BlockException e){
        return BaseResult.error("同一个类的兜底方法");
    }



}
